const Koa = require('koa')
const parser = require('koa-bodyparser')
const Router = require('koa-router')
const requireDirectory = require('require-directory')
class InitApp {
    static app = null;
    static createApp(){
        InitApp.app = new Koa();
        InitApp.app.use(parser())
        InitApp.initRouter();
        InitApp.app.listen(3000,()=>{
            console.log('server is running http://localhost:3000')
        })
    }

    static initRouter(){
        requireDirectory(module,process.env.PWD+'/api',{visit:InitApp._whenModuleLoad})
    }
    static _whenModuleLoad(obj){
        if(obj instanceof Router){
            InitApp.app.use(obj.routes()).use(obj.allowedMethods())
        }
    }
}

module.exports = InitApp